define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupons/clinic/index' + location.search,
                    add_url: 'coupons/clinic/add',
                    edit_url: 'coupons/clinic/edit',
                    del_url: 'coupons/clinic/del',
                    multi_url: 'coupons/clinic/multi',
                    import_url: 'coupons/clinic/import',
                    table: 'coupons_clinic',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true},
                        {field: 'name', title: __('NameText'), operate: 'LIKE'},
                        {field: 'user_id', title: __('User_id'),sortable:true},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'phonetext', title: __('Phonetext'), operate: 'LIKE'},
                        {field: 'qrcodeimage', title: __('Qrcodeimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'logoavatar', title: __('Logoavatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user_code', title: __('User_code'),formatter: function (value, row, index) {
                                return '<span style="color:#605ca8">' + value + '</span>';
                            }},
                        {field: 'user_password', title: __('User_password')},
                        {field: 'province.name', title: __('Province'), operate: 'LIKE'},
                        {field: 'city.name', title: __('City'), operate: 'LIKE'},
                        {field: 'area.name', title: __('Area'), operate: 'LIKE'},
                        {field: 'addresstext', title: __('Addresstext'), operate: 'LIKE'},
                        {field: 'longitudetext', title: __('Longitudetext'), operate: 'LIKE'},
                        {field: 'latitudetext', title: __('Latitudetext'), operate: 'LIKE'},
                        
                        {field: 'coverpicimages', title: __('Coverpicimages'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                       
                        {field: 'weigh', title: __('Weigh'),sortable:true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            Controller.api.addressChoose();
        },
        edit: function () {
             Controller.api.bindevent();
             Controller.api.addressChoose();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            addressChoose:function(){
                $("[data-toggle='addresspicker']").data("callback", function(res){
                 //其中res则是包含了address/lat/lng等信息的JSON对象
                 console.log(res);
                 $('#c-latitudetext').val(res.lat);
                 $('#c-longitudetext').val(res.lng);
                 if($('#c-addresstext').val()==''){
                   $('#c-addresstext').val(res.address);  
                 }
                });
            }
           
        }
    };
    return Controller;
});